/* ============================================================================ */
/*                   Import and Preprocess Data                                 */
/* ============================================================================ */

clear all // Clear all stored data and variables

/* Import CSV file containing P2P loan data */
import delimited "$input\P2P_new_loans.csv"

/* Convert date column to Stata date format */
gen time = date(date_arrete, "YMD")
format time %td
gen mdate = mofd(time) // Convert to month-year format for consistency

/* Keep only data within the specified time window */
keep if mdate < mofd(date("20200101", "YMD"))
keep if mdate > mofd(date("20151231", "YMD"))

/* ============================================================================ */
/*                      Part I.1: Summary Statistics                            */
/* ============================================================================ */

//----------------------------------------------------------------------------//
				* Table 1: FinTech loan characteristics *
//----------------------------------------------------------------------------//

/* Generate a unique ID for each firm-lender pair */
egen id = group(siren declarant)

/* Remove duplicate observations at the firm-month level */
duplicates drop id mdate, force 

/* Define key variable for initial loan amount */
gen montant_initial = encours_prete

/* Define a global variable for investor-related statistics */
global loan_var `"nb_contributeur_bq part_pret_bq nb_contributeur_pm part_pret_pm nb_contributeur_pp part_pret_pp"'

/* Compute summary statistics for FinTech loan characteristics */
eststo loan_stat: estpost tabstat $loan_var, s(min mean p50 max sd count) c(statistics)

/* ============================================================================ */
/*                  Label Variables for Readability                             */
/* ============================================================================ */

label var nb_contributeur_bq "Number of banks" 
label var part_pret_bq       "Share of banks" 
label var nb_contributeur_pm "Number of legal entities" 
label var part_pret_pm       "Share of legal entities" 
label var nb_contributeur_pp "Number of individuals" 
label var part_pret_pp       "Share of individuals"

/* Add horizontal space to labels for aesthetic purposes in LaTeX table */
foreach v of varlist $loan_var {
	label variable `v' `"\hspace{0.5em}`:variable label `v''"'
}

/* ============================================================================ */
/*                  Table 1 - Investors                                         */
/* ============================================================================ */

esttab loan_stat using "$tables\Table_1_FinTech_loans_characteristics_new_b.tex", ///
replace nonote nogaps booktabs fragment label noobs nomtitles nonumbers ///
cells("min(fmt(2 2 0 0 2 0 2 0 2)) mean(fmt(2 2 0 0 2 0 2 0 2)) ///
p50(fmt(2 2 0 0 2 0 2 0 2)) max(fmt(2 2 0 0 2 0 2 0 2)) ///
sd(fmt(2 2 0 0 2 0 2 0 2)) count(fmt(%9.0fc))") ///
collabels("Min" "Mean" "p50" "Max" "S.D." "Count", pattern(1 1 1 1 1 1) ///
prefix(\multicolumn{@span}{c}{) suffix(}) span ) ///
refcat(nb_contributeur_bq "\\ \emph{Investors}", nolabel) 

